package com.wajahatkarim3.rdbexplorer.slice.dialog;

import com.wajahatkarim3.rdbexplorer.ResourceTable;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Picker;
import ohos.agp.window.dialog.CommonDialog;
import ohos.app.Context;

import java.util.ArrayList;

/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
public class PickerDialog extends CommonDialog implements Component.ClickedListener {
    private Context contextSlice;
    private PickerdialogListener listener;
    private Picker picker;
    private String getdata;
    private int defaultIndex;
    private String[] getStr;
    Component layout;

    public PickerDialog(Context context, PickerdialogListener listener, ArrayList<String> list, int defaultIndex) {
        super(context);

        this.contextSlice=context;
        this.listener=listener;
        getStr = list.toArray(new String[0]);
        this.defaultIndex = defaultIndex;
    }

    @Override
    protected void onCreate() {
        super.onCreate();

        layout = LayoutScatter.getInstance(contextSlice).
                parse(ResourceTable.Layout_picker_dialog, null, true);
        setTransparent(true);
        setContentCustomComponent(layout);
        initview();
    }

    private void initview() {
        picker= (Picker)layout.findComponentById(ResourceTable.Id_data_picker);
        Button cancelbtn= (Button) layout.findComponentById(ResourceTable.Id_picker_cancel_btn);
        Button affirmbtn= (Button) layout.findComponentById(ResourceTable.Id_picker_affirm_btn);
        cancelbtn.setClickedListener(this);
        affirmbtn.setClickedListener(this);
        picker.setDisplayedData(getStr);
        if (this.defaultIndex >= 0 && (getStr != null && getStr.length > 0)) {
            picker.setValue(this.defaultIndex);
            getdata = getStr[this.defaultIndex];
        }
        picker.setValueChangedListener(new Picker.ValueChangedListener() {
            @Override
            public void onValueChanged(Picker picker, int i, int i1) {}
        });

        picker.setValueChangedListener((picker1, oldVal, newVal) -> {
            getdata=getStr[newVal];
        });
    }

    @Override
    public void onClick(Component component) {
        if (component.getId() == ResourceTable.Id_picker_affirm_btn) {
            this.listener.getPickerStrSuccess(getdata);
        }
        PickerDialog.this.destroy();
    }
}
